<?php
session_start();

// 如果session中不存在 username 就表示登录
if(!isset($_SESSION['username'])){
	// header("location: login.php");
	// die("未登录<script>setTimeout(function(){window.location.href='login.php'},3000)</script>");}
	die("<script>alert('未登录'); window.location.href='../login.php';</script>");
}

include("../conn.php");


if ($_SERVER['REQUEST_METHOD'] == 'POST') {

	$title = $_POST['title'];
	$content = $_POST['content'];
	
	$sql = "insert into articles(title, content) values(:title,:content)";
	
	// 连接数据库
	$pdo = getConn();
	
	// 预编译sql语句
	$stmt = $pdo->prepare($sql);
	
	// 绑定参数
	$stmt->bindParam(":title", $title);
	$stmt->bindParam(":content", $content);
	
	// 执行sql语句
	$stmt->execute();
	
	// 判断是否成功
	if($stmt->rowCount()>0) {
		header("location: list.php");
		exit();
	} else {
		die("添加失败");
	}
}

?>
<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title></title>
</head>
<body>
	<form action="" method="post">
		<div>文章标题：</div>
		<div><input type="text" name="title"></div>
		<div>文章内容：</div>
		<textarea name="content" hidden="hidden" id="content" cols="30" rows="10"></textarea>
		<div id="editor"></div>
		<input type="submit" value="添加文章">
	</form>
	<script src="https://unpkg.com/wangeditor/dist/wangEditor.min.js"></script>
	<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
	<script>
		const editor = new window.wangEditor("#editor");
		
		// 获取textarea元素
		const content = $('#content')
		
		// 第二步，监控变化，同步更新到 textarea
		editor.config.onchange = function (html) {
			content.val(html)
		}
		editor.create();
	</script>
</body>
</html>
